package com.example.joke.custom.shape58

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import androidx.appcompat.widget.LinearLayoutCompat
import com.example.joke.R

class LoadingView : LinearLayoutCompat {

    private lateinit var mShape: ShapeView
    private lateinit var mShadow: View
    private var mTranslationY: Float = 0F
    private var mDuration: Long = 500
    // 代码优化1之后，动画仍然在执行
    private var mIsStopAnimation = false

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        mTranslationY = dp2px(50F)
        initLayout()
    }

    private fun dp2px(dp: Float): Float {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.displayMetrics)
    }

    // 初始化加载布局
    private fun initLayout() {
        // 1. 加载写好的布局：layout_loading_view
        // 1.1 实例化view：View loadView = inflate(getContextO,R.layout.ui_loading_view, null)
        // 1.2 添加到该View：addView(loadView)
        // this：代表把 layout_loading_view 加载到 LoadingView 中
        inflate(context, R.layout.layout_loading_view, this)
        mShape = findViewById(R.id.shape)
        mShadow = findViewById(R.id.shadow)

        // 代码优化2
        // onCreate 方法中执行，布局文件解析，反射创建实例
        // startFallAnimation()
        post {
            // onResume 之后View绘制流程执行完毕之后
            startFallAnimation()
        }
    }

    private fun startFallAnimation() {
        if (mIsStopAnimation) return

        val translationY = ObjectAnimator.ofFloat(mShape, "translationY", 0f, mTranslationY)
        val scaleX = ObjectAnimator.ofFloat(mShadow, "scaleX", 1f, 0.3f)

        val animatorSet = AnimatorSet()
        animatorSet.duration = mDuration
        animatorSet.playTogether(translationY, scaleX)
        animatorSet.interpolator = AccelerateInterpolator()
        // 是一种思想，在Adapter中的 BannerView 写过，如果不想实现所有接口方法，只想实现需要的那一个接口方法
        animatorSet.addListener(object : AnimatorListenerAdapter() {

            override fun onAnimationEnd(animation: Animator) {
                // 改变形状，先改变形状再开始动画
                mShape.exchangeShape()
                // 下落万之后就上抛了，监听动画执行完毕
                startUpAnimation()
//                mShape.exchangeShape()

            }
        })
        animatorSet.start()
    }

    private fun startUpAnimation() {
        if (mIsStopAnimation) return

        val translationY = ObjectAnimator.ofFloat(mShape, "translationY", mTranslationY, 0f)
        val scaleX = ObjectAnimator.ofFloat(mShadow, "scaleX", 0.3f, 1f)

        val animatorSet = AnimatorSet()
        animatorSet.duration = mDuration
        animatorSet.playTogether(translationY, scaleX)
        animatorSet.interpolator = DecelerateInterpolator()
        // 是一种思想，在Adapter中的 BannerView 写过，如果不想实现所有接口方法，只想实现需要的那一个接口方法
        animatorSet.addListener(object : AnimatorListenerAdapter() {

            override fun onAnimationStart(animation: Animator) {
                // 上抛开始时，执行旋转动画
                startRotationAnimation()
            }

            override fun onAnimationEnd(animation: Animator) {
                // 下落万之后就上抛了，监听动画执行完毕
                startFallAnimation()
            }
        })
        animatorSet.start()
    }

    private fun startRotationAnimation() {
        var rotation: ObjectAnimator? = null
        rotation = when (mShape.getCurrentShape()) {
            ShapeView.Shape.Circle,ShapeView.Shape.Square -> ObjectAnimator.ofFloat(mShape, "rotation", 0f, 180f)
            ShapeView.Shape.Triangle -> ObjectAnimator.ofFloat(mShape, "rotation", 0f, 120f)
        }
        rotation.duration = mDuration
        rotation.interpolator = DecelerateInterpolator()
        rotation.start()
    }

    // 代码优化1
    override fun setVisibility(visibility: Int) {
        super.setVisibility(View.INVISIBLE) // 不要再去摆放和计算，少走一些系统的源码，view的绘制流程
        // 清理动画
        mShape.clearAnimation()
        mShadow.clearAnimation()
        // 把LoadingView从父布局移除
        val parent: ViewGroup = getParent() as ViewGroup
        if (parent != null) {
            parent.removeView(this) // 从父布局移除
            removeAllViews()    // 移除自己所有的view
        }
        mIsStopAnimation = true
    }
}